<!DOCTYPE html>
<html>
  <head>
  	<div th:include="base-libs :: base-libs" />
    <title>项目互斥规则设置-新增/修改</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <style>
    	.outDiv{
    		width:40%;
    		height:300px;
    		border:2px solid #ccc;
    		margin-top:7px;
    		margin-left:7%;
    		float:left;
    		border-radius:5px;

    	}
    	.titleDiv{
    		width:100%;
    		height:40px;
    		line-height:40px;
    		padding-left:30px;
    		font-size:17px;
    		font-weight:bold;
    	}
    	.inDiv{
    		width:90%;
    		height:70%;
    		margin:0 auto;
    		border:1px solid #ddd;
    		margin-top:10px;
			overflow: auto;
			overflow-x: hidden;
    	}
    	#lastTb td{
    		background:#fff;
    		border:0;
    	}
    	.hidden{
    		display:none;
    	}
    </style>
    
    <script th:inline="javascript">
	    /**读取request中的初始化数据*/
	    var requestData = [[${requestData}]];
	</script>
	<script>
		$(function(){
			$(this).registerManagerPage({
				cacheTables: ['TB_ZY_JCXM'],
				callback: initView
			});
// 			initView();
// 			initData();
		})
		
		function initView(){
			//保存
			$("#btnBc").click(function(){
				if($(".form-control").QuickValidate()){
					
					var bpcXmlbChecks = $('#bpcXmlbTrr').tree('getChecked');
					if(StringUtils.isEmpty($("#XMDM").val())){
						$.QuickAlert.alertFail({content: '请选择一个项目列表！'});
					}else if(bpcXmlbChecks.length==0){
						$.QuickAlert.alertFail({content: '请至少选择一个被排斥项目列表！'});
					}else{
						var bhcxmArr = [];
						$.each(bpcXmlbChecks, function(i,item){
							bhcxmArr.push({XMDM: item.XMDM, XMMC: item.XMMC });
						})
						$.QuickRemote.AjaxFiledFormSubmit("hcgzForm", saveHandler, {bhcxm: JSON.stringify(bhcxmArr)});
					}
				}
			})
			
			//返回
			$("#btnFh").click(function(){
				$.QuickUrlUtils.redirectUrl("index.html");
			})
			
			//一级科室代码
			$("#KSDM").change(function(){
				var optionText = $(this).children(':selected').text();
				if(optionText=='请选择') optionText = '';
				$("#KSMC").val(optionText);
			})
			
			/** 医疗机构combogrid */
			var readonly = !requestData["isManager"];
			$("#combogridYljg").combogrid({
	            height: 30
	            ,width: 300
	            ,panelWidth: 500
	            ,singleSelect: true
	            ,pagination: true
	            ,idField: 'PTJGID'
	            ,textField: 'YLJGMC'
	            ,mode: "remote"
	            ,readonly: readonly
	            ,hasDownArrow: !readonly
	            ,url: "/console/yljgxx/queryPageSummary.service"
	            ,delay: 1000
	            ,reversed: false
	            ,columns:[[
	                {field:'YLJGMC',title:'机构名称',width:240},
	                {field:'XZQHMC',title:'行政区划',width:80},
	                {field:'WSJGLBMC',title:'机构类别',width:80},
	                {field:'YYDJMC',title:'机构等级',width:80}
	            ]]
	            ,onBeforeLoad: function(queryParams) {
	                var url = $(this).datagrid("options")["url"];
					/*URL不等空时再发送请求*/
	                if (!readonly) {
	                    queryParams["pageSize"] = queryParams["rows"];
	                    queryParams["pageNumber"] = queryParams["page"];
	                    queryParams["YLJGMC"] = queryParams["q"];
	                    var result = [];
	                    $("#KSDM").createOptions(result,{textField : "KSMC" , valueField : "KSDM"});
	                    return true;
	                }else{
	                    return false;
	                }
	            },
	            onClickRow : function(rowIndex, rowData){
	                initKsxx(rowData);
	            },
	            onLoadSuccess : function(data){
// 					alert(JSON.stringify(data))
				}
	        });
			
			initData();
		}
		
		function initData(){
			initXmlb();
			
			var hcgzData = StoreCache.removeCache('hcgzData');
			if(!$.isEmptyObject(hcgzData)){
				$("#combogridYljg").combogrid("setValue", hcgzData);
				initKsxx(hcgzData);
				$("#type").val('update');
				$(".form-control").each(function(){
					var name = $(this).attr('name');
					$(this).val(hcgzData[name]);
				})
				
				//选中项目列表
				var xmNode = $('#xmlbTrr').tree('find', hcgzData.XMDM);
				$('#xmlbTrr').tree('select', xmNode.target);
				//查询被排斥项目
				$.QuickRemote.AjaxJson("queryBpcxm", function(result){
					if(result.state){
						$.each(result.data, function(){
							xmNode = $('#bpcXmlbTrr').tree('find', this.XMDM);
							$('#bpcXmlbTrr').tree('check', xmNode.target);
						})
					}else{
						$.QuickAlert.alertFail({content: result.info});
					}
		        }, {GZBH: hcgzData.GZBH});
			}else{
                if(!requestData["isManager"]){
                    $("#combogridYljg").combogrid("grid").datagrid("loadData", [requestData]);
                    $("#combogridYljg").combogrid("setValue", requestData);
                    delete  requestData["KSDM"];
                    initKsxx(requestData);
                }
			}
		}
		
		/** 保存回调 */
		function saveHandler(result){
			if(result.state){
				$.QuickAlert.alertNormal(result.info, {callback: function(){
					$("#btnFh").trigger("click");
				}});
			}else{
				$.QuickAlert.alertFail({content: result.info});
			}
		}
		
		/** 初始化一级科室下拉框 */
		function initKsxx(params2){
			var params = $.extend({},params2);//处理乱码
			$("#YLJGDM").val(params.YLJGDM);
			$("#YLJGMC").val(params.YLJGMC);
			$("#PTJGID").val(params.PTJGID);
	        $.QuickRemote.AjaxJson("/console/ksxx/", function(result){
	            $("#KSDM").createOptions(result,{textField : "KSMC" , valueField : "KSDM"});
                $("#KSDM").val(params.KSDM);
	        }, params);
		}
		
		/** 载入项目列表 */
		function initXmlb(){
			var xmlbTbData = StoreCache.getCache('TB_ZY_JCXM');
			if(!$.isEmptyObject(xmlbTbData)){
				
				var list = createTreeList(xmlbTbData.data, null);
				//先关闭所有节点再打开根节点
				$('#xmlbTrr').tree('loadData', list).tree("collapseAll").tree('expand',$('#xmlbTrr').tree('getRoot').target)
							.tree({
								onClick:function(node){
									$("#XMDM").val(node.id);
									$("#XMMC").val(node.text);
								}
							});
				
				var bpcXmlbTbData = StoreCache.getCache('TB_ZY_JCXM');
				var list2 = createTreeList(bpcXmlbTbData.data, null);
				$('#bpcXmlbTrr').tree('loadData', list2).tree("collapseAll").tree('expand',$('#bpcXmlbTrr').tree('getRoot').target);
			}
		}
		
		/** 树-数据处理 */
		function createTreeList(list, sjjgdm) {
			var childList = [];
			for (var i = 0; i < list.length; i++) {
				var item = list[i];
				if (item == null) {
					continue;
				}
				item["id"] = item.XMDM;
				item["text"] = item.XMMC;
				item["state"] = "open";
				var sjjgdm1 = item["SJDM"];
				if (sjjgdm1 != sjjgdm) {
					continue;
				} else {
					var resid = item["XMDM"];
					var lt = createTreeList(list, resid);
					item["children"] = lt;
					childList.push(item);
					list[i] = null;
				}
			}
			if (childList.length == 0) {
				childList = null;
			}
			return childList;
		}
	</script>
  </head>
  
  <body class="easyui-layout">
    <div region="center">
    	<h4 class="h4-title" style="text-align:center;margin:0;padding:0;font-size: 18px;">
			项目互斥规则设置
		</h4>
		<form action="save" id="hcgzForm">
			<input id="type" name="type" value="add" class="hidden"/>
			<input name="GZBH" class="form-control hidden" />
<!-- 		<select name="YLTDID" class="condition" cacheTable="TB_ZY_YYDJ" style="width:91%"> -->
<!-- 							</select> -->
	    	<table class="form-table">
				<tr>
					<td class="td-text titleTd"><label>规则名称</label></td>
					<td class="td-content">
						<input name="GZMC" class="form-control" 
						quick-config="label:'规则名称', validateProxy:'notNull length', length: '256'" />
					</td>
					<td class="td-text titleTd"><label>医疗机构</label></td>
					<td class="td-content">
						<input id="combogridYljg" />
						
						<input id="YLJGMC" name="YLJGMC" class="form-control hidden" 
						quick-config="label:'医疗机构', validateProxy:'notNull'" />
						<input id="PTJGID" name="PTJGID" class="form-control hidden" />
						<input id="YLJGDM" name="YLJGDM" class="form-control hidden" />
					</td>
					<td class="td-text titleTd"><label>科室</label></td>
					<td class="td-content">
						<select id="KSDM" name="KSDM" class="form-control" quick-config="label:'科室', validateProxy:'notNull'">
							<option value="">请选择</option>
						</select>
						
						<input id="KSMC" name="KSMC" class="form-control hidden"/>
					</td>
					<td class="td-text titleTd"><label>状态</label></td>
					<td class="td-content">
						<select name="ZT" class="form-control">
							<option value="1">启用</option>
							<option value="2">停用</option>
						</select>
					</td>
				</tr>
			</table>
			
			<div style="margin:0 auto;margin-top:10px;">
				<div class="outDiv">
					<div class="titleDiv">项目列表</div>
					<div class="inDiv">
						<input id="XMMC" name="XMMC" class="form-control hidden"/>
						<input id="XMDM" name="XMDM" class="form-control hidden"/>
						<ul id="xmlbTrr" class="easyui-tree" checkbox="false"></ul>
					</div>
				</div>
				<div class="outDiv">
					<div class="titleDiv">被排斥项目列表</div>
					<div class="inDiv">
						<ul id="bpcXmlbTrr" class="easyui-tree" checkbox="true"></ul>
					</div>
				</div>
			</div>
			<table class="form-table" style="width:95%;margin-left:-29px;" id="lastTb">
				<tr>
					<td class="td-text titleTd"><label>规则描述</label></td>
					<td class="td-content">
						<input name="GZMS" class="form-control" 
						quick-config="label:'规则描述', validateProxy:'length', length: '256'"/>
					</td>
					<td class="td-text titleTd"><label>排斥时间</label></td>
					<td class="td-content">
						<input name="PCSJ" class="form-control" placeholder="单位：天"
						quick-config="label:'排斥时间', validateProxy:'notNull length num', length: '3'"/>天
					</td>
				</tr>
			</table>
		</form>
    </div>
    
    <div region="south" style="height:50px;line-height:50px;text-align:center;overflow:hidden;border:0;border-top:1px solid #ddd">
    	<button id="btnBc" class="btn btn-info" type="button">保存</button>
    	<button id="btnFh" class="btn btn-info" type="button">返回</button>
    </div>
  </body>
</html>
